// pages/coursehourse/question/index.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    options:{},
    page:{},
    height:0,
    flag:true,
    questionContent:'',
    buttonsubmit:'',
    questionId:'',
    userInfo:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          options: options,
          height: res.windowHeight,
          userInfo:app.globalData.userInfo
        })
      }
    })
    var _this=this;
    app.paiRequest("/app/course/question/findPage", { courseHoursId: options.courseHourId,pageNo:1,pageSize:10 }).then(function (res) {
      console.log(res);
      var page=res.data;
      _this.setData({
        page:page
      })
    });

  },
  onScrollLower:function(){
    var _this = this;
    var page=this.data.page;
    var list = page.list;
    var count = page.count;
    var lent = list.length;
    if (count == lent) {
      wx.showToast({
        title: '到底了',
        icon: 'success',
        duration: 1000
      })
    } else {
      app.paiRequest("/app/course/question/findPage", { courseHoursId: options.courseHourId, pageNo: page.pageNo + 1, pageSize: page.pageSize }).then(function (res) {
        var currentPage = res.data;
        var pageList = list.concat(currentPage.list);
        page.list = pageList;
        page.pageNo = currentPage.pageNo;
        _this.setData({
          page: page
        })
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 遮罩层隐藏
  conceal: function () {
    this.setData({ flag: true })
  },
  addquestion:function(){
    this.setData({ 
      buttonsubmit:'buttonsubmit',
      flag: false 
      })
  },
  questionContent:function(e){
    this.setData({
      questionContent:e.detail.value
    })
  },
  buttonsubmit:function(e){
    var questionContent = this.data.questionContent;
    var userInfo = app.globalData.userInfo;
    var options = this.data.options;
    var _this=this;
    var submitData={
      askUserId:userInfo.id,
      userName: userInfo.userName,
      userAvatarUrl: userInfo.avatar,
      courseId: options.courseId,
      courseName: options.courseName,
      content: questionContent,
      courseHoursId: options.courseHourId,
      courseHoursName: options.courseHourName      
    };
    
    app.paiRequest("/app/course/question/save", submitData).then(function (res) {
      console.log(res);
      _this.onLoad(options);
      _this.setData({
        flag: true
      })
    })
  },
  answer:function(e){
    var id=e.currentTarget.dataset.id;
    this.setData({
      buttonsubmit: 'answersubmit',
      flag: false,
      questionId: id,
    })
  },
  answersubmit:function(){
    var questionContent = this.data.questionContent;
    var userInfo = app.globalData.userInfo;
    var options = this.data.options;
    var questionId = this.data.questionId;
    var _this = this;
    var submitData = {
      questionId: questionId,
      teacherId: userInfo.id,
      teacherName: userInfo.userName,
      teacherAvatarUrl: userInfo.avatar,
      content: questionContent,
    };
    app.paiRequest("/app/course/answers/save", submitData).then(function (res) {
      _this.onLoad(options);
      _this.setData({
        flag: true
      })
    })
  }
})